package com.example.mydemo.entity.domain;

import com.example.mydemo.entity.Menu;

import java.time.format.DateTimeFormatter;

public class MenuDTO {
    private Long authorityId;    // 对应后端menuId
    private String authorityName= "";// 对应后端title
    private Integer orderNumber; // 对应后端sort
    private String menuUrl = "";      // 对应后端href
    private String menuIcon = "";     // 对应后端icon
    private String createTime;   // 格式化后的创建时间
    private String authority = "";    // 权限字段
    private Integer checked = 0; // 默认未选中
    private String updateTime;   // 格式化后的更新时间
    private Integer isMenu;      // 是否菜单（根据业务逻辑设置）
    private Long parentId;       // 对应后端parentId

    // 静态转换方法：从Menu实体转换为MenuDTO
    public static MenuDTO from(Menu menu) {
        MenuDTO dto = new MenuDTO();
        // 基础字段映射
        dto.setAuthorityId(menu.getMenuId());
        dto.setAuthorityName(menu.getTitle());
        dto.setOrderNumber(menu.getSort());
        dto.setMenuUrl(menu.getHref());
        dto.setMenuIcon(menu.getIcon());
        if(menu.getParentId()==0){
            dto.setParentId((long) -1);
        }else{
            dto.setParentId(menu.getParentId());
        }
        dto.setAuthority(menu.getPermission());

        // 时间格式化（格式：yyyy/MM/dd HH:mm:ss）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        if (menu.getCreateTime() != null) {
            dto.setCreateTime(menu.getCreateTime().format(formatter));
        }
        if (menu.getUpdateTime() != null) {
            dto.setUpdateTime(menu.getUpdateTime().format(formatter));
        }

        // 业务逻辑：isMenu（假设menuType=1表示是菜单）
        if (menu.getMenuType() != null && menu.getMenuType() == 1) {
            dto.setIsMenu(1); // 是菜单
        } else {
            dto.setIsMenu(0); // 非菜单
        }

        return dto;
    }

    // Getter和Setter方法（省略，实际需补全）
    public Long getAuthorityId() { return authorityId; }
    public void setAuthorityId(Long authorityId) { this.authorityId = authorityId; }
    public String getAuthorityName() { return authorityName; }
    public void setAuthorityName(String authorityName) { this.authorityName = authorityName; }
    public Integer getOrderNumber() { return orderNumber; }
    public void setOrderNumber(Integer orderNumber) { this.orderNumber = orderNumber; }
    public String getMenuUrl() { return menuUrl; }
    public void setMenuUrl(String menuUrl) { this.menuUrl = menuUrl; }
    public String getMenuIcon() { return menuIcon; }
    public void setMenuIcon(String menuIcon) { this.menuIcon = menuIcon; }
    public String getCreateTime() { return createTime; }
    public void setCreateTime(String createTime) { this.createTime = createTime; }
    public Object getAuthority() { return authority; }
    public void setAuthority(String authority) { this.authority = authority; }
    public Integer getChecked() { return checked; }
    public void setChecked(Integer checked) { this.checked = checked; }
    public String getUpdateTime() { return updateTime; }
    public void setUpdateTime(String updateTime) { this.updateTime = updateTime; }
    public Integer getIsMenu() { return isMenu; }
    public void setIsMenu(Integer isMenu) { this.isMenu = isMenu; }
    public Long getParentId() { return parentId; }
    public void setParentId(Long parentId) { this.parentId = parentId; }
}
